package com.iwhalecloud.bss.kite.cucc.web.controller.menu;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.menu.ICuccMenuService;

import edu.emory.mathcs.backport.java.util.Arrays;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "/cuccMenu - 系统菜单")
@RestController
@RequestMapping("/cuccMenu")
public class CuccMenuController {

    private static final List<String> GRAY_FLAGS = Lists.newArrayList("g", "r", "a", "y");

    private static final String CONTEXT_PATH_GRAY = "-gray";
    private static final String NEW_CONTEXT_PATH_GRAY = "/gray";

    /**
     灰度的判断条件
     */
    private static final String VERSION_STR = "VERSION=2.21.3";

    @Resource
    private ICuccMenuService cuccMenuService;

    @ApiOperation(value = "跳转到菜单路径", notes = "跳转到菜单路径")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "menuId", value = "菜单标识", required = true)
    })
    @GetMapping("/toMenu/{menuId}")
    public void toMenu(@PathVariable("menuId") String menuId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String menuPath = cuccMenuService.getMenuPath(menuId);
        if (StringUtils.isNotBlank(menuPath)) {
            // 判断cookie中的灰度标识
            // 20210625废弃cookie的方式-List<Cookie> cookies = (List<Cookie>) Arrays.asList(request.getCookies()).stream().filter(cookie -> (KiteStringUtils.equals(((Cookie) cookie).getName(), "kite_token") && grayFlags.contains(((Cookie) cookie).getValue().substring(2, 3)))).collect(Collectors.toList());
            // 根据新的方式 head返回信息中判断VERSION=2.21.3则认为是灰度工号
            String headStr = request.getHeader("CBSS-MS-ZQZTGRAY");
            if (headStr == null) {
                headStr = "";
            }
            boolean versionFlag = headStr.contains(VERSION_STR);
            // 如果是灰度工号，contextpath增加-gray
            // 20210625废弃old的方式 if (!KiteListUtils.isEmpty(cookies)) {
            if (versionFlag) {
                String contextPath = "";
                if (menuPath.startsWith("http")) {
                    int aa = menuPath.indexOf("//") + 2;
                    String menuPathh = menuPath.substring(aa, menuPath.length());
                    int start = menuPathh.indexOf("/");
                    int end = menuPathh.indexOf("/", start + 1);
                    contextPath = menuPathh.substring(start, end);
                }
                else {
                    int start = menuPath.indexOf("/");
                    int end = menuPath.indexOf("/", start + 1);
                    contextPath = menuPath.substring(start, end);
                }
                menuPath = menuPath.replace(contextPath, contextPath +  NEW_CONTEXT_PATH_GRAY);
            }
            String queryString = request.getQueryString();
            String path = StringUtils.isNotBlank(queryString) ? menuPath + SymbolConsts.QUESTION_MARK + queryString
                : menuPath;
            response.sendRedirect(path);
        }
    }
}
